package ict;

import ict.mobile.R;
import android.app.Activity;
import android.os.Bundle;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.*;
import android.view.*;
import android.widget.*;

public class orderDisplayActivity extends Activity {
	SQLiteDatabase db;
	String sql;
	Cursor cursor = null;
	String[] columns = { "mid", "name", "password", "age" };
	String dataStrHeader = String.format("%4s %-12s %-9s %3s\n", "Mid", "Name",
			"Password", "Age");
	String dataStr;
	TextView displayOrderTV;

	public static final int modfiy = Menu.FIRST;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.orderdisplay);

		findViewBy();
		selectAllFromproductOrder();
	}

	// ////find view by ID
	public void findViewBy() {
		displayOrderTV = (TextView) findViewById(R.id.displayOrderTV);

	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, modfiy, 0, "Order Modify").setIcon(
				R.drawable.edit);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		super.onOptionsItemSelected(item);
		switch (item.getItemId()) {
		case modfiy: {
			Intent intent = new Intent();
			intent.setClass(this, modifyorderActivity.class);
			startActivity(intent);
			break;
		}

		}
		return true;
	}

	// ///display productOrder
	public void selectAllFromproductOrder() {
		try {
			db = SQLiteDatabase.openDatabase("/data/data/ict.mobile/icsDB",
					null, SQLiteDatabase.OPEN_READONLY);

			cursor = db.rawQuery("select * from productOrder", null);

			// (OrderID text PRIMARY KEY, ShopName text, StaffID int, date text,
			// LoginName text, prod01 int, prod02 int , prod03 int , prod04 int,
			// prod05 int);";
			// ShopCode, ShopName, ShopManager
			// String dataStr = String.format("%4s %-10s %7s\n", "ShopCode",
			// "ShopName", "ShopManager");
			String dataStr = "";
			while (cursor.moveToNext()) {
				// or cursor.getInt(0);
				String OrderID = cursor.getString(0);
				String ShopCode = cursor.getString(1);
				int StaffID = cursor.getInt(2);
				String date = cursor.getString(3);
				String LoginName = cursor.getString(4);
				String prod01 = cursor.getString(5);
				String prod02 = cursor.getString(6);
				String prod03 = cursor.getString(7);
				String prod04 = cursor.getString(8);
				String prod05 = cursor.getString(9);
				// or cursor.getString(3);
				/*
				 * String ProductName =
				 * cursor.getString(cursor.getColumnIndex("ProductName"));
				 * 
				 * int Quantity =
				 * cursor.getInt(cursor.getColumnIndex("Quantity"));
				 * 
				 * // or cursor.getString(4);
				 */
				// dataStr += String.format("%11d %-6s %4s\n", ProductCode,
				// ProductName, Quantity);
				dataStr += "Order ID: "+OrderID + '\t'+"ShopName: "+ShopCode + '\n' +"StaffID: "+StaffID +'\t'+ "LoginName: "+LoginName +'\n'+ "Date: "+date
						+ '\n'+"Prod01: "+prod01 + '\n'+"Prod02: "+prod02 + '\n'+"Prod03: "+ prod03 + '\n'+"Prod04: "+prod04 + '\n'+"Prod05: "+prod05 + '\n'
						+ "-------------------------------------------"+'\n';
			}
			displayOrderTV.setText(dataStr);

			db.close();

		} catch (SQLiteException e) {
			Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
		}

	}
    @Override
    protected void onRestart() {
        super.onRestart();
        selectAllFromproductOrder();
        Toast.makeText(this, "Order Modify Successfull", Toast.LENGTH_SHORT).show();
    }
}
